package controller;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.alibaba.fastjson.JSON;
import common.jsondata.IResponseJSONResult;
import common.jsondata.impl.ResponseJSONResultImpl;
import entity.Role;
import entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.impl.RoleServiceImpl;
import service.impl.UserServiceImpl;
import util.snowflake.UUIDGenerator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * controller返给前端进行一个展示
 */
@Controller
@RequestMapping(value="/role")
public class RoleController {
    @Autowired
    private RoleServiceImpl roleService;

    private static IResponseJSONResult responseJSONResult = new ResponseJSONResultImpl();

    private Logger logger = LoggerFactory.getLogger(RoleController.class);

    //跳转角色列表
    @RequestMapping("/roleinfo")
    public String tologin(){ return  "views/role/roleinfo"; }

    //跳转详情页面
    @RequestMapping("/roledetail")
    //@RequiresRoles("administrator")
    public String roledetail(){ return  "views/role/roledetail"; }

    //跳转修改页面
    @RequestMapping("/rolemodify")
    public String rolemodify(){ return  "views/role/rolemodify"; }

    //查询角色信息
    @ResponseBody
    @RequestMapping(value="/selectrole")
    public String selectRole(HttpServletRequest request, HttpServletResponse response) {
        List<Role> list = roleService.findAllRole();
        Map<String,Object> map = new HashMap<>();
        if(list.size() > 0){
            map.put("code",0);
            map.put("msg","查询成功");
            map.put("count",list.size());
            map.put("data",list);
        }else {
            map.put("code",1);
            map.put("msg","未查询到相关数据");
            map.put("count",list.size());
            map.put("data",list);
        }
        logger.debug("数据：" + JSON.toJSONString(map) );
        responseJSONResult.setResponseUTF8(response);
        return JSON.toJSONString(map);
    }

    /**
     * 录入角色的信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addRole")
    public String addRole(HttpServletRequest request, HttpServletResponse response){
        String roleCode = request.getParameter("roleCode");
        String roleName = request.getParameter("roleName");
        System.out.println(roleName);
        String status = request.getParameter("status");

        Role role = new Role();
        role.setRoleId(UUIDGenerator.getUUID());
        role.setRoleCode(roleCode);
        role.setRoleName(roleName);
        role.setStatus(status.toCharArray()[0]);
        int count = roleService.insertOne(role);

        responseJSONResult.setResponseUTF8(response);
        //3.跳转查询页面
        return "redirect:/role/roleinfo";
    }

    /**
     * 录入角色的信息
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleterole")
    public String deleterole(HttpServletRequest request, HttpServletResponse response){
        String roleId = request.getParameter("roleId");

        int count = 0;
        //根据主键删除用户
        count = roleService.deleteRoleByID(roleId);
        if(count>0){
            return responseJSONResult.ReturnJSONResunt(response,"删除成功(>ω･* )ﾉ");
        }else{
            return responseJSONResult.ReturnJSONResunt(response,"删除失败(╯﹏╰)b");
        }
        //返回删除信息
        //return "redirect:/role/roleinfo";
    }


}
